﻿@using Microsoft.AspNetCore.Components.Forms
@using Shared.Designer.Palette
@using Shared.Extensions
@using SqlSugar
@inherits PropertiesComponentBase
<Collapse OnCollapseChanged="OnCollapseChanged">
    <CollapseItems>
        <CollapseItem Text="通用属性">
            <BootstrapInput @bind-Value="ComponentData.Id" ShowLabel="true" Readonly="true" DisplayText="ID"></BootstrapInput>
            <BootstrapInput @bind-Value="ComponentData.Name" ShowLabel="true"  DisplayText="名字"></BootstrapInput>
            <BootstrapInputNumber @bind-Value="@ComponentData.Width" IsSelectAllTextOnFocus="true" OnValueChanged="OnValueChangedForInt" ShowLabel="true" DisplayText="宽" ShowButton="true" TValue="int" Min="0" Max="12" />
            <BootstrapInputNumber @bind-Value="@ComponentData.Height" IsSelectAllTextOnFocus="true" OnValueChanged="OnValueChangedForInt" ShowLabel="true" DisplayText="高" ShowButton="true" TValue="int" Min="0" Max="12" />
           <div>
                <Collapse>
                    <CollapseItems>
                        <CollapseItem Text="多语言显示">
                            <ValidateForm Model="@ComponentData.MutLanguage">
                                <BootstrapInput @bind-Value="@ComponentData.MutLanguage.zh_CN" ShowLabel="true" DisplayText="中文" TValue="string" OnValueChanged="OnChangeString"></BootstrapInput>
                                <BootstrapInput @bind-Value="@ComponentData.MutLanguage.en_US" ShowLabel="true" DisplayText="英文" TValue="string" OnValueChanged="OnChangeString"></BootstrapInput>
                                <BootstrapInput @bind-Value="@ComponentData.MutLanguage.zh_TW" ShowLabel="true" DisplayText="繁体" TValue="string" OnValueChanged="OnChangeString"></BootstrapInput>
                                <BootstrapInput @bind-Value="@ComponentData.MutLanguage.ja_JP" ShowLabel="true" DisplayText="日语" TValue="string" OnValueChanged="OnChangeString"></BootstrapInput>
                            </ValidateForm>
                        </CollapseItem>
                    </CollapseItems>
                </Collapse>
           </div>
        </CollapseItem>
        <CollapseItem Text="扩展属性">
            <Switch @bind-Value="@ComponentData.Props["ReadOnly"].BoolVal" OnValueChanged="OnValueChangedForBool" ShowLabel="true" DisplayText="是否为只读"></Switch>

        </CollapseItem>
        <CollapseItem Text="事件和数据源">
            <Select TValue="string" @bind-Value="@ComponentData.Props["StoreName"].StringValue" IsVirtualize="true" OnQueryAsync="OnQueryAsync" ShowSearch="true" DisplayText="存储过程名字"></Select>
            <CheckboxList IsButton="true" TValue="IEnumerable<string>" Items="@BoxListData" @bind-Value="@ComponentData.Props["Params"].StringListValue" ShowLabel="true" DisplayText="存储过程需要的参数" />
        </CollapseItem>
    </CollapseItems>
</Collapse>
@code {
    [NotNull]
    private Modal? Modal { get; set; }

    [NotNull]
    private ValidateForm? ValidateForm { get; set; }
    [Inject]
    [NotNull]
    private DialogService? DialogService { get; set; }

    [Inject]
    [NotNull]
    private ISqlSugarClient? db { get; set; }

    [Parameter]
    public EventCallback<ChangeEventArgs> OnLabelChanged { get; set; }
    [Parameter]
    public EventCallback<ChangeEventArgs> OnWidthChanged { get; set; }
    [Parameter]
    public EventCallback<ChangeEventArgs> OnHeightChanged { get; set; }
    [Parameter]
    public EventCallback<PInof> OnPropChanged { get; set; }
    /// <summary>
    /// Primary / Secondary / Success / Danger / Warning / Info / Dark
    /// </summary>
    // List<SelectedItem> ColorItems = new List<SelectedItem>() {
    //             new SelectedItem("None", "None"),
    //             new SelectedItem("Active", "Active"),
    //             new SelectedItem("Primary", "Primary"),
    //             new SelectedItem("Warning", "Warning"),
    //             new SelectedItem("Info", "Info"),
    //             new SelectedItem("Danger", "Danger"),
    //             new SelectedItem("Success", "Success"),
    //             new SelectedItem("Dark", "Dark")
    //             };
    // List<SelectedItem> BoolItems = new List<SelectedItem>() {
    //             new SelectedItem("True", "True"),
    //             new SelectedItem("False", "False"),

    //             };
    // private SelectedItem? VirtualItem1 { get; set; }
    private async Task<QueryData<SelectedItem>> OnQueryAsync(VirtualizeQueryOption option)
    {
        await Task.Delay(200);
        var storeList = db.DbMaintenance.GetProcList("Test6");
        var sourse = storeList.Select(x => new SelectedItem() { Text = x, Value = x }).ToList();
        return new QueryData<SelectedItem> { Items = sourse, TotalCount = sourse.Count };
        // return new QueryData<SelectedItem>
        //     {
        //         Items = new List<SelectedItem>()
        //                 {
        //             new SelectedItem("北京", "北京"),
        //             new SelectedItem("上海", "上海") { Active = true },
        //         },
        //         TotalCount = 2
        //     };
    }
    // public List<SelectedItem> Parameters { get; set; } = new List<SelectedItem>() { new SelectedItem() { Value = "1", Text = "22" }, new SelectedItem() { Value = "2", Text = "22" } };
    // public List<ContainerDto> Containers;
    List<TreeViewItem<TreeComponentData>>? treeData;
    List<SelectedItem>? BoxListData;
    protected override Task OnInitializedAsync()
    {
        //         treeData = new List<TreeComponentData>() { new TreeComponentData()
        //         {
        //             Type= ControlType.Comtainer,
        //             Key=FormDesigner.ContainerData.Id,
        //             Title=FormDesigner.ContainerData.Name,
        //             Children=new List<TreeComponentData>()
        // } };
        treeData = new List<TreeViewItem<TreeComponentData>>();
        BoxListData = new();
        FormDesigner.FunctionPage.ContainerData.ToTree(treeData);
        FormDesigner.FunctionPage.ContainerData.ToSelectedItemList(BoxListData);
        // Parameters = FormDesigner.ContainerData.FindAllComponent().Select(x => new SelectedItem() { Text = "   " + x.ParentId + x.Id, Value = x.Id }).ToList();
        // Containers = FormDesigner.ContainerData.FindAllContainer();
        return base.OnInitializedAsync();
    }
    public async Task OnCollapseChanged(CollapseItem e)
    {
        if (e.Text == "事件和数据源" && e.IsCollapsed == false)
        {
            BoxListData = new();
            FormDesigner.FunctionPage.ContainerData.ToSelectedItemList(BoxListData);
            await FormDesigner.StateHasChangedAsync();
        }
    }
    // public Task OnNodeLoadDelayAsync(AntDesign.TreeEventArgs<TreeComponentData> args)
    // {
    //     var dataItem = args.Node.DataItem;
    //     // if (dataItem.Children==null)
    //     // {
    //     //     return Task.CompletedTask;
    //     // }
    //     switch (dataItem.Type)
    //     {
    //         case ControlType.Comtainer:
    //             ContainerDto? res = null;
    //             FormDesigner.FunctionPage.ContainerData.FindContainer(x => x.Id == dataItem.Key, ref res);
    //             if (res != null)
    //             {
    //                 var node = (List<TreeComponentData>)dataItem.Children;
    //                 node.Clear();
    //                 foreach (var row in res.Rows)
    //                 {
    //                     node.Add(new TreeComponentData()
    //                         {
    //                             Type = ControlType.Row,
    //                             Key = row.Id,
    //                             Title = row.Name
    //                         });
    //                 }
    //             }

    //             break;
    //         case ControlType.Row:
    //             RowDto? resrow = null;
    //             FormDesigner.FunctionPage.ContainerData.FindRow(x => x.Id == dataItem.Key, ref resrow);
    //             if (resrow != null)
    //             {
    //                 var node = (List<TreeComponentData>)dataItem.Children;
    //                 node.Clear();
    //                 foreach (var com in resrow.ComponentList)
    //                 {
    //                     node.Add(new TreeComponentData()
    //                         {
    //                             Type = ControlType.Component,
    //                             Key = com.Id,
    //                             Title = com.Name
    //                         });
    //                 }
    //             }
    //             break;
    //         case ControlType.Component:
    //             ComponentDto? resrom = null;
    //             FormDesigner.FunctionPage.ContainerData.FindComponent(x => x.Id == dataItem.Key, ref resrom);
    //             if (resrom != null)
    //             {
    //                 var node = (List<TreeComponentData>)dataItem.Children;
    //                 node.Clear();
    //                 foreach (var com in resrom.ChildContainers)
    //                 {
    //                     node.Add(new TreeComponentData()
    //                         {
    //                             Type = ControlType.Comtainer,
    //                             Key = com.Id,
    //                             Title = com.Name
    //                         });
    //                 }
    //             }
    //             break;
    //         default:
    //             break;
    //     }
    //     return Task.CompletedTask;
    // }
    private IEnumerable<SelectedItem> GetParameters()
    {
        return FormDesigner.FunctionPage.ContainerData.FindAllComponent().Select(x => new SelectedItem() { Text = "   " + x.Id, Value = x.Id });

    }
    private IEnumerable<SelectedItem> GetProList()
    {
        return db.DbMaintenance.GetProcList("LCM").Select(x => new SelectedItem() { Text = x, Value = x });
    }
    public void OnValueChanged(MouseEventArgs e)
    {

        //Color.Dark
    }
    public void OnFieldValueChanged(string key, object value)
    {//lstNames.GroupBy(n => n).Any(c => c.Count() > 1);
        if (key == "Id")
        {

            if (FormDesigner.FunctionPage.ContainerData.FindAllComponent(x => true).GroupBy(x => x.Id).Any(x => x.Count() > 0))
            {
                ValidateForm.SetError("Id", "数据库中已存在");
            }

        }
        //return Task.CompletedTask;
    }
    private Task OnValidComplexModel(EditContext context)
    {
        ValidateForm.SetError("Id", "数据库中已存在");
        return Task.CompletedTask;
    }
    /// <summary>
    /// 当设置控件的Id属性的时候
    /// </summary>
    /// <param name="v"></param>
    /// <returns></returns>
    public Task IdOnValueChanged(string v)
    {

        if (FormDesigner.FunctionPage.ContainerData.FindAllComponent(x => true).GroupBy(x => x.Id).Any(x => x.Count() > 1))
        {
            ComponentData.Id = "";
        }
        return Task.CompletedTask;
    }
    /// <summary>
    /// 仅仅是手动触发界面刷新
    /// </summary>
    /// <param name="v"></param>
    /// <returns></returns>
    public async Task OnValueChangedForBool(bool v)
    {
        await FormDesigner.StateHasChangedAsync();
    }
    /// <summary>
    /// 当设置控件外观颜色的时候
    /// </summary>
    /// <param name="v"></param>
    /// <returns></returns>
    public async Task OnValueChangedForColor(Color v)
    {
        await FormDesigner.StateHasChangedAsync();
    }
    /// <summary>
    /// 仅仅是手动触发界面刷新
    /// </summary>
    /// <param name="v"></param>
    /// <returns></returns>
    public async Task OnValueChangedForInt(int v)
    {
        await FormDesigner.StateHasChangedAsync();
    }
    /// <summary>
    /// 刷新主界面
    /// </summary>
    /// <param name="v"></param>
    /// <returns></returns>
    public async Task OnValueChangedForString(string v)
    {
        await FormDesigner.StateHasChangedAsync();
    }
    public  Task OnChangeString(string a)
    {
        // await FormDesigner.StateHasChangedAsync();
        StateHasChangedOnContainer?.Invoke();
        return Task.CompletedTask;
    }
    public async Task OnChange()//OnChangeTreeSelects
    {
        await FormDesigner.StateHasChangedAsync();
    }
    public async Task OnChangeTreeSelects(IEnumerable<TreeComponentData> list)//OnChangeTreeSelects
    {
        var ss = FormDesigner.FunctionPage.ContainerData;
        await FormDesigner.StateHasChangedAsync();
    }
}
